package org.lboot.flow.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.lboot.core.auth.AuthService;
import org.lboot.core.auth.anno.CheckRole;
import org.lboot.core.domain.ResponseDTO;
import org.lboot.flow.FlowCtl;
import org.lboot.flow.module.assign.FlowTaskAssign;
import org.lboot.flow.module.assign.FlowTaskAssignService;
import org.lboot.flow.module.assign.params.FlowTaskAssignCreateParams;
import org.lboot.flow.module.assign.params.FlowTaskAssignQueryParams;
import org.lboot.jpa.utils.JpaDataUtil;
import org.lboot.starter.plugin.rsr.RsrTree;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("flow")
@Api(tags = "流程审核指派")
@AllArgsConstructor
@RsrTree("流程引擎")
public class FlowTaskAssignController {
    // 鉴权服务
    AuthService authService;

    FlowTaskAssignService assignService;
    // 流程控制器
    FlowCtl flowCtl;

    @PostMapping("assign")
    @ApiOperation(value = "审核指派新增")
    @CheckRole(orPerm = "flow:assign:create")
    public ResponseDTO<Object> assignCreate(@Validated @RequestBody FlowTaskAssignCreateParams params){

        return ResponseDTO.succData(
                flowCtl.createAssign(
                        params.getFlowCode(),
                        params.getFlowNodeId(),
                        params.getAssignType(),
                        params.getAssignId()
                )
        );
    }

    @DeleteMapping("assign/{id}")
    @ApiOperation(value = "审核指派删除")
    @CheckRole(orPerm = "flow:assign:delete")
    public ResponseDTO<Object> assignDelete(@PathVariable("id") String id){
        flowCtl.deleteAssign(id);
        return ResponseDTO.succMsg("删除成功");
    }

    @GetMapping("assign")
    @ApiOperation(value = "审核指派列表")
    public ResponseDTO<Object> assignQueryList(FlowTaskAssignQueryParams params){
        Pageable pageable = JpaDataUtil.buildPageable(params);
        FlowTaskAssign data = new FlowTaskAssign();
        BeanUtil.copyProperties(params,data, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<FlowTaskAssign> example = Example.of(data);
        return ResponseDTO.succData(
                assignService.getAll(pageable,example)
        );
    }

    @GetMapping("assign/{id}")
    @ApiOperation(value = "审核指派查询")
    public ResponseDTO<Object> assignQuery(@PathVariable("id") String id){
        return ResponseDTO.succData(
                assignService.getOneUnwrap(id)
        );
    }
}
